import { verifyUser, verifyCheckUser } from '@/api/user.js';
export default {
	onShow() {
		//e签宝身份核验结果查询
		const options = uni.getEnterOptionsSync()
		if (options && options.referrerInfo && 
			options.referrerInfo.extraData && 
			options.referrerInfo.extraData.faceResult && 
			options.referrerInfo.extraData.faceResult.ErrorCode === 0
		) {
			this.getVerifyCheckUser(this.orderKey)
		} else if ((options && options.referrerInfo && options.referrerInfo.extraData && options.referrerInfo
				.extraData.faceResult && options.referrerInfo.extraData.faceResult.ErrorCode !== 0)) {
			return this.$util.Tips({
				title: options.referrerInfo.extraData.faceResult.ErrorMsg
			});
		}
	},
	methods:{
		getVerifyUser(orderKey){
			let provider = null
			const that = this
			
			// #ifdef MP-WEIXIN
			provider = 'WE_CHAT_FACE'
			// #endif
			
			// #ifdef MP-ALIPAY
			provider = 'mpalipay'
			// #endif
			
			uni.showLoading({ title: '开始人脸识别', mask: true })

			verifyUser({ orderkey:orderKey, provider ,
				store_id: this.deliveryType == 1 ? 0 :this.system_store.id||0,
			}).then(res => {
				uni.hideLoading()
				
				// #ifdef H5
				this.$util.Tips({ title: 'H5还未接入' })
				// #endif
				
				// #ifdef MP-WEIXIN
				uni.navigateToMiniProgram({
					appId: 'wx1cf2708c2de46337', // 公证签小程序APPID
					path: '/pages/face/index?bizToken=' + res.data.apply.faceToken, // 刷脸页面地址
					fail: (res) => { return this.$util.Tips({ title: '取消验证' }) }
				})
				// #endif
				
				// #ifdef MP-ALIPAY
				my.startAPVerify({
					url: res.data.apply.originalUrl,
					certifyId: res.data.apply.faceToken,
					complete: (res) => {
						console.log(res);
						if(res.resultStatus === "9000") {
							this.getVerifyCheckUser(orderKey)
						} else {
							this.$util.Tips({  title: '验证失败：' + res.resultStatus })
						}
					}
				})
				// #endif
				
			}).catch(err=>{
				uni.hideLoading()
				this.$util.Tips({ title: err });
			})
		},
		getVerifyCheckUser(orderKey) {
			uni.showLoading({ title: '获取结果' })
			// 订单详情，等待人脸认证结果
			if(this.faceCheck){
				this.faceCheck='start'
			}
			verifyCheckUser({
				orderkey:orderKey
			}).then(res => {
				uni.hideLoading()
				
				const status = res.data.status
				if(status == 'SUCCESS') {
					this.$util.Tips({ title: '验证成功！'})
					// if(this.verifyOk) setTimeout(() => this.verifyOk(), 1500)
					if(this.verifyOk) {
						this.verifyOk()
					}
				} else {
					this.$util.Tips({ title: '验证失败' })
				}

			}).catch(err => {
				uni.hideLoading()
				return this.$util.Tips({ title: err });
			})
		},
		getVerifyCheck(orderKey) {
			return new Promise(resolve => {
				verifyCheckUser({ orderkey:orderKey }).then(res => {
					console.log(res);
					resolve(true)
				}, err => {
					resolve(false)
				})
			})
		}
	}
}